﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DAL_V1.App_Code.DBBroker;
using EntityObjects;
using BAL.Controller;

namespace PresentationLayer.View.DepartmentManager
{
    public partial class DelegateRepresentativeAuthority : System.Web.UI.Page
    {
               List<CollectionPoint> cp_list;
               private static List<Employee> depEmp_list;
               private static List<Employee> empe_list;
               private static List<Department> dep_list;
               Employee loginDepHead;
      
        protected void Page_Load(object sender, EventArgs e)
        {   
          
            if (!IsPostBack)
            {
                loginDepHead = new Employee();
           
                loginDepHead = (Employee)Session["CurrentUser"];
               
                
                /*Current Collection Point and Current Rep*/
                CollectionPoint c_Obj = new CollectionPoint();
                Employee currentRep=new Employee();
                cp_list = new List<CollectionPoint>();
               
                if (loginDepHead.DeptId != null)
                {
                    c_Obj = DelRepresentativeController.getDelRepAuthorityController().getCollectionPointByDeptId(loginDepHead.DeptId.ToString());
                    currentRep = DelRepresentativeController.getDelRepAuthorityController().getEmployeebyDeptIdAndRole(loginDepHead.DeptId.ToString(), 3);
                }
                if (c_Obj != null && currentRep!=null)
                {
                    lblCollectionPoint.Text = c_Obj.Place.ToString();
                    lblCurrentRep.Text = currentRep.EmpName.ToString();
                }
                else
                {
                    lblCollectionPoint.Text = "NIL";
                    lblCurrentRep.Text = "NIL";
                }
                
          
                    ViewState["curRep"] = currentRep;

                /*Display Collection Point in DropDownList*/
                cp_list = DelRepresentativeController.getDelRepAuthorityController().selectAllCollectionPoint();
                ddlNewColPoint.DataSource = cp_list;
                ddlNewColPoint.DataTextField = "Place";
                ddlNewColPoint.DataValueField = "CollectionId";
                ddlNewColPoint.DataBind();
          

                empe_list = new List<Employee>();
                depEmp_list = new List<Employee>();
                dep_list = new List<Department>();
                // **** respective Department Employee list display in GridView *************

                dep_list = DelRepresentativeController.getDelRepAuthorityController().selectAllDepartment();
                if (loginDepHead.DeptId != null)
                {
                    depEmp_list = DelRepresentativeController.getDelRepAuthorityController().selectEmployeeByDept(loginDepHead.DeptId.ToString());
                    empe_list = DelRepresentativeController.getDelRepAuthorityController().getEmployeeListByNotIncludeEmpIdAndRoleId(loginDepHead.DeptId.ToString(), loginDepHead.EmpId.ToString(), 3);
                }
             
                  GVEmpList.DataSource = empe_list;
                  GVEmpList.DataBind();
            }
              
            
         
        }


        protected void btnSearch_Click(object sender, EventArgs e)
        {
                 Employee empe = new Employee();
                  
                 empe = (Employee)Session["CurrentUser"];
                 string Depname = empe.DeptId;
                 GVEmpList.DataSource = DelRepresentativeController.getDelRepAuthorityController().getEmployeebyDeptIdAndEmpName(Depname, txtEmployee.Text.Trim()); 
                 GVEmpList.DataBind();

        }

        protected void txtEmployee_TextChanged(object sender, EventArgs e)
        {
          

        }

        protected void btnselect_Click(object sender, EventArgs e)
        {

                  LinkButton EmpIdButton = sender as LinkButton;
                  int employeeid = Convert.ToInt32(EmpIdButton.CommandArgument);
                  EmpIdButton.Enabled = false;
          
                  ViewState["selEmp"] = Convert.ToString(employeeid);
                  lblmessage.Visible = true;
                  lblmessage.Text = "Employee ID '" + ViewState["selEmp"] + "' has been selected.";

          
        }

         protected void btnDelegate_Click(object sender, EventArgs e)
         {
                    string selEmp;
                    string selDep="";
                    string crep_id;
                    string Toaddress = "";
                    Employee empe = new Employee();


                    empe = (Employee)Session["CurrentUser"];
                    if (empe.DeptId != null) selDep = empe.DeptId.ToString();
               
                  /*find Grid row object from empe_List by selected employeeid*/
                  if (ViewState["selEmp"]==null)
                   {
                       lblmessage.Visible = true;
                       lblmessage.Text="There is no Employee to assign.";
                   }
                  else
                  {
                        selEmp = Convert.ToString(ViewState["selEmp"]);
                        if (depEmp_list.Count != 0)
                          {
                              Employee myLocatedObj = depEmp_list.Find(delegate(Employee ep) { return ep.EmpId == selEmp; });

                             /*Upate DeptRep RoleId "3" for new assigned  person at Employee table*/
                               myLocatedObj.RoleId = 3;
                               int result = DelRepresentativeController.getDelRepAuthorityController().updateEmployee(myLocatedObj, selEmp);
                               Toaddress = myLocatedObj.Email.ToString();

                             /*update old Dept-Rep to normal Employee role*/
                               Employee crep_Obj = new Employee();

                              if (ViewState["curRep"] != null)
                              {
                                 crep_Obj = (Employee)ViewState["curRep"];
                                 crep_Obj.RoleId = 5;
                                 crep_id = crep_Obj.EmpId.ToString();
                                 int cresult = DelRepresentativeController.getDelRepAuthorityController().updateEmployee(crep_Obj, crep_id);

                              }
                           }
                           if (dep_list.Count != 0)
                           {
                              /*update newDeptRep in Department table*/
                              Department myDeptObj = dep_list.Find(delegate(Department dep) { return dep.DeptId == selDep; });
                              myDeptObj.RepresentativeId = selEmp;
                              myDeptObj.CollectionId = Convert.ToInt32(ddlNewColPoint.SelectedValue.ToString());
                              int uresult = DelRepresentativeController.getDelRepAuthorityController().updateDepartment(myDeptObj, selDep);
                              lblmessage.Text = "Successfully Delegated to Employee ID '" + ViewState["selEmp"] + "'.";
                              btnDelegate.Visible = false;
                             /*Notify email to assigned person*/
                              Send_email(Toaddress);
                          
                          }
                   }
             }
            
           private void Send_email(string To)
           {    Email semail = new Email("thwenusoe@gmail.com");
                semail.createMail();
            }


    }
}